package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserService;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.task.FlowTaskInstApproverVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskInstApproverRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowTaskInstApprover;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowTaskInstApproverExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowTaskInstApproverDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: niechanggang
 * @date:2021/9/2 11:21
 * @description:
 */
@Repository
public class FlowTaskInstApproverRepositoryImpl  implements FlowTaskInstApproverRepository {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FlowTaskInstApproverDAO  flowTaskInstApproverDAO;

    @Autowired
    private UserService userService;

    @Override
    public int insert(FlowTaskInstApproverVo flowTaskInstApproverVo) {
        FlowTaskInstApprover flowTaskInstApprover = new FlowTaskInstApprover();
        BeanUtils.copyProperties(flowTaskInstApproverVo, flowTaskInstApprover);
        int cnt = flowTaskInstApproverDAO.insertSelective(flowTaskInstApprover);
        return cnt;
    }

    @Override
    public List<FlowTaskInstApproverVo> selectListByTaskId(String taskId) {
        List<FlowTaskInstApproverVo> voList =  new ArrayList<>();
        List<FlowTaskInstApprover> approverList =  flowTaskInstApproverDAO.selectListByTaskId(taskId);
        for(FlowTaskInstApprover approver : approverList){
            FlowTaskInstApproverVo approverVo = new FlowTaskInstApproverVo();
            BeanUtils.copyProperties(approver, approverVo);
            // 根据userId获取员工工号
            UserInfo operatorUser = userService.getUserInfoObj(approver.getUserId());
            approverVo.setUserName(operatorUser == null ? "" : operatorUser.getUserName());
            voList.add(approverVo);
        }
        return voList;
    }

    @Override
    public void updateReadFlag(String taskId, String  userId, Integer readFlag) {
        FlowTaskInstApprover  flowTaskInstApprover  = new   FlowTaskInstApprover();
        flowTaskInstApprover.setReadFlag(readFlag);
        FlowTaskInstApproverExample query = new FlowTaskInstApproverExample();
        FlowTaskInstApproverExample.Criteria  criteria = query.createCriteria();
        criteria.andTaskIdEqualTo(taskId);
        criteria.andUserIdEqualTo(userId);
        flowTaskInstApproverDAO.updateByExampleSelective(flowTaskInstApprover, query);
    }

    @Override
    public void todoTaskupdateReadFlag(String taskId, String userId, Integer readFlag) {
        FlowTaskInstApprover  flowTaskInstApprover  = new   FlowTaskInstApprover();
        flowTaskInstApprover.setReadFlag(readFlag);
        FlowTaskInstApproverExample query = new FlowTaskInstApproverExample();
        FlowTaskInstApproverExample.Criteria  criteria = query.createCriteria();
        criteria.andTaskIdEqualTo(taskId);
        criteria.andUserIdEqualTo(userId);
        flowTaskInstApproverDAO.updateByExampleSelective(flowTaskInstApprover, query);
    }

    @Override
    public Integer countReadedTask (List<String> taskIds){
       return flowTaskInstApproverDAO.countReadedTask(taskIds);
    }

    @Override
      /**
     * 更新
     *
     * @param entity 实体
     * @return 是否成功
     */
    public boolean updateTaskInstApprovereById(FlowTaskInstApproverVo flowTaskTraceVo){
        FlowTaskInstApprover entity = new FlowTaskInstApprover();
        BeanUtils.copyProperties(flowTaskTraceVo, entity);
        flowTaskInstApproverDAO.updateByPrimaryKeySelective(entity);
        return true;
    }
}
